package com.myschool.server.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myschool.server.manager.TimetableManager;
import com.myschool.server.model.Json;
import com.myschool.server.model.Rows;
import com.myschool.server.model.TimeTableEntry;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;

@Controller
public class TimeTableController {

	@Autowired
	TimetableManager timeTableManager;

	@RequestMapping("timeTableDetails.htm")
	public @ResponseBody
	Json TimeTableDetails(HttpServletRequest request, String sidx, String sord,
			int page, int rows, Boolean _search, String searchField,
			String searchOper, String searchString) {

		Json jsonData = new Json();
		try

		{

			List<TimeTableEntry> timetable = timeTableManager
					.retrieveTimeTableBySection(0);

			int recordCount = timetable.size();

			System.out
					.println("Teacher count : " + String.valueOf(recordCount));
			jsonData.page = page;
			jsonData.total = recordCount;
			jsonData.record = recordCount;

			jsonData.rows = new Rows[recordCount];
			for (int count = 0; count < recordCount; count++) {
				TimeTableEntry user = timetable.get(count);
				Rows row = new Rows();
				row.i = count + 1;
				// row.cell = new String[] { user.getId().toString(),
				// String.valueOf( user.getMonday()), String.valueOf(
				// user.getTuesday()), String.valueOf(
				// user.getWednesday()),String.valueOf( user.getThursday()),
				// String.valueOf( user.getFriday()),String.valueOf(
				// user.getSaturday()), String.valueOf( user.getSunday()) };
				jsonData.rows[count] = row;

			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

		return jsonData;
	}

	@RequestMapping(value = "addEditTimeTable.htm", method = RequestMethod.POST)
	public void addTimetable(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			System.out.println(request.getParameter("mon"));

			int Mon = Integer.valueOf(request.getParameter("mon"));
			int Tue = Integer.valueOf(request.getParameter("tue"));
			int Wed = Integer.valueOf(request.getParameter("wed"));
			int Thus = Integer.valueOf(request.getParameter("thus"));
			int Fri = Integer.valueOf(request.getParameter("fri"));
			int Sat = Integer.valueOf(request.getParameter("sat"));
			int Sun = Integer.valueOf(request.getParameter("sun"));

			String operation = request.getParameter("oper");
			TimeTableEntry timetable = new TimeTableEntry();

			/*
			 * timetable.setMonday(Mon); timetable.setTuesday(Tue);
			 * timetable.setWednesday(Wed); timetable.setThursday(Thus);
			 * timetable.setFriday(Fri); timetable.setSaturday(Sat);
			 * timetable.setSunday(Sun);
			 */

			if (operation.trim().equals("add")) {

				this.timeTableManager.create(timetable);
			} else {

				String idStr = request.getParameter("timetableID");
				Long ID = Long.parseLong(idStr);
				timetable.setEntityId(ID);
				this.timeTableManager.update(timetable);
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

	}

	@RequestMapping("deleteTimeTableRecord.htm")
	public void deleteTimeTableRecord(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String userIDParameter = request.getParameter("timetableID");
			String[] userIDArray = userIDParameter.split(";");
			TimeTableEntry table;
			for (String userID : userIDArray) {
				if (userID == "")
					continue;
				System.out.println("Dee user:" + userID);
				table = new TimeTableEntry();
				Long id = Long.parseLong(userID.trim());
				table.setEntityId(id);
				this.timeTableManager.delete(table);
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

	}
}
